<?php
namespace app\api\controller;

use app\api\model\Configs;
use app\api\model\MemberOrders;
use app\api\model\User;

class Merchant extends Base
{
    // 不需要验证
    protected $excludeValidateCheck = [
        'exchangeInformation',
    ];

    // 定义自动实例化模型
    protected $ModelPath = 'Merchant';

    public function information(){
//        background: '',
//        infos: {
//            avatar: '', nickname: '', rows: [], level_name: '', idents: [],
//            qr: {enable: true, text: '', alias: 'none', params: {}}
//        },
//        search: {enable: false, placeholder: '', button: ''},
//        ad: false,
//        invite: {enable: false, title: '', nickname: '', mobile: ''},
//        counts: {enable: false,
//            lists: [{name: '', count: '', type: ''}]
//        },
//        lists: {enable: false, title: '',
//            filters: {type: '', search_keyword: '', page: 1}
//        }
        $userId = request()->UserModel->id;
        $infos = User::with(['userLevel', 'userVirtualLevel'])->where('id', $userId)->find();
        $parentInfos = User::where('id', $infos['pid'])->find();
        $inviteStatus = true;
        if(empty($parentInfos)){
            $parentInfos['id'] = '';
            $parentInfos['phone'] = '';
            $inviteStatus = false;
        }
        $count = User::where('pid', $userId)->count();
        $str = '%|' . $userId . '|%';
        $teams_merchant_count = User::where('status', 'on')
            ->whereLike('parent_ids', $str)
            ->where('merchant_status', 'on')
            ->count();

        $is_merchant = false;
        $merchant_score = 0;
        if($merchant_info = $this->M->where('user_id', $userId)->where('status', 'on')->find())
        {
            $is_merchant = true;
            $merchant_score = bcdiv_amount($merchant_info['score'], \app\model\Base::$hundred);
        }
        //整理数据
        $data = [
            'main_title'=>'门店管理',
            'background'=>get_oss_url('/static/teams-bg.png'),
            'infos'=>[
                'avatar'=>get_oss_url('/static/logo.png'),
                'nickname'=>$infos['phone'],
                'rows'=>[
                    'ID：' . $infos['id'],
                    '手机号码：' . $infos['phone'],
                    '邀请码：' . $infos['invite_code'],
                ],
                'level_name'=>$infos['virtual_level_name'],
                'idents'=>[],
                'qr'=>[
                    'enable'=>true,
                    'text'=>'',
                    'alias'=>'none',
                    'params'=>[],
                ],
            ],
            'search'=>[
                'enable'=>false,
                'placeholder'=>'',
                'button'=>'',
            ],
            'ad'=>false,
            'invite'=>[
                'enable'=>$inviteStatus,
                'title'=>'我的邀请人',
                'nickname'=>'ID：' . $parentInfos['id'],
                'mobile'=>'',//$parentInfos['phone'],
            ],
            'counts'=>[
                'enable'=>true,
                'lists'=>[
//                    [
//                        'enable'=>true,
//                        'name'=>'团队数量',
//                        'count'=>$count,
//                        'type'=>'',
//                        'alias'=>'',//'teams.size_zone',
//                        'params'=>[],
//                        'action'=>''
//                    ],
//                    [
//                        'enable'=>true,
//                        'name'=>'直推数量',
//                        'count'=>$count,
//                        'type'=>'',
//                        'alias'=>'',//'teams.size_zone',
//                        'params'=>[],
//                        'action'=>''
//                    ],
//                    [
//                        'enable'=>true,
//                        'name'=>'加盟店数量',
//                        'count'=>$teams_merchant_count,
//                        'type'=>'',
//                        'alias'=>'',//'teams.size_zone',
//                        'params'=>[],
//                        'action'=>''
//                    ],
                    [
                        'enable'=>$is_merchant,
                        'name'=>'积分转化',
                        'count'=>$merchant_score,
                        'type'=>'',
                        'alias'=>'merchant.exchange',
                        'params'=>[],
                        'action'=>''
                    ],
                ],
            ],
            'lists'=>[
                'enable'=>true,
                'title'=>'',
                'filters'=>[
                    'type'=>'',
                    'search_keyword'=>'',
                    'page'=>1,
                ]
            ]
        ];
        //响应数据
        return showSuccess($data);
    }

    public function activationLists()
    {
        $user_id = request()->UserModel->id;
        if($merchant_info = $this->M->where('user_id', $user_id)->find())
        {
            $lists = MemberOrders::with(['user_info', 'user_level'])
                ->where('merchant_id', $merchant_info['id'])
                ->where('type', 'member')
//                ->append(['user_info'])
                ->append(['values'])
                ->select();
            //整理数据
            $data = [
                'lists'=>$lists
            ];
            //响应数据
            return showSuccess($data);
        }else{
            return showError('不存在该商户');
        }
    }

    public function franchiseInformation(){
        $userId = request()->UserModel->id;
        $infos = User::with(['userLevel', 'userVirtualLevel'])->where('id', $userId)->find();
        $parentInfos = User::where('id', $infos['pid'])->find();
        $inviteStatus = true;
        if(empty($parentInfos)){
            $parentInfos['id'] = '';
            $parentInfos['phone'] = '';
            $inviteStatus = false;
        }
        $count = User::where('pid', $userId)->count();
        $str = '%|' . $userId . '|%';
        $teams_merchant_count = User::where('status', 'on')
            ->whereLike('parent_ids', $str)
            ->where('merchant_status', 'on')
            ->count();

        $is_merchant = false;
        $merchant_score = 0;
        if($merchant_info = $this->M->where('user_id', $userId)->where('status', 'on')->find())
        {
            $is_merchant = true;
            $merchant_score = bcdiv_amount($merchant_info['score'], \app\model\Base::$hundred);
        }
        //整理数据
        $data = [
            'main_title'=>'门店管理',
            'background'=>get_oss_url('/static/teams-bg.png'),
            'infos'=>[
                'avatar'=>get_oss_url('/static/logo.png'),
                'nickname'=>$infos['phone'],
                'rows'=>[
                    'ID：' . $infos['id'],
                    '手机号码：' . $infos['phone'],
                    '邀请码：' . $infos['invite_code'],
                ],
                'level_name'=>$infos['virtual_level_name'],
                'idents'=>[],
                'qr'=>[
                    'enable'=>true,
                    'text'=>'',
                    'alias'=>'none',
                    'params'=>[],
                ],
            ],
            'search'=>[
                'enable'=>false,
                'placeholder'=>'',
                'button'=>'',
            ],
            'ad'=>false,
            'invite'=>[
                'enable'=>$inviteStatus,
                'title'=>'我的邀请人',
                'nickname'=>'ID：' . $parentInfos['id'],
                'mobile'=>'',//$parentInfos['phone'],
            ],
            'counts'=>[
                'enable'=>false,
                'lists'=>[
//                    [
//                        'enable'=>true,
//                        'name'=>'团队数量',
//                        'count'=>$count,
//                        'type'=>'',
//                        'alias'=>'',//'teams.size_zone',
//                        'params'=>[],
//                        'action'=>''
//                    ],
//                    [
//                        'enable'=>true,
//                        'name'=>'直推数量',
//                        'count'=>$count,
//                        'type'=>'',
//                        'alias'=>'',//'teams.size_zone',
//                        'params'=>[],
//                        'action'=>''
//                    ],
//                    [
//                        'enable'=>true,
//                        'name'=>'加盟店数量',
//                        'count'=>$teams_merchant_count,
//                        'type'=>'',
//                        'alias'=>'',//'teams.size_zone',
//                        'params'=>[],
//                        'action'=>''
//                    ],
//                    [
//                        'enable'=>$is_merchant,
//                        'name'=>'积分转化',
//                        'count'=>$merchant_score,
//                        'type'=>'',
//                        'alias'=>'merchant.exchange',
//                        'params'=>[],
//                        'action'=>''
//                    ],
                ],
            ],
            'lists'=>[
                'enable'=>true,
                'title'=>'',
                'filters'=>[
                    'type'=>'',
                    'search_keyword'=>'',
                    'page'=>1,
                ]
            ]
        ];
        //响应数据
        return showSuccess($data);
    }

    public function franchiseLists()
    {
        $user_id = request()->UserModel->id;
        if($merchant_info = $this->M->where('user_id', $user_id)->find())
        {
            $lists = MemberOrders::with(['user_info', 'user_level'])
                ->where('merchant_id', $merchant_info['id'])
                ->where('type', 'merchant')
//                ->append(['user_info'])
                ->append(['values'])
                ->select();
            //整理数据
            $data = [
                'lists'=>$lists
            ];
            //响应数据
            return showSuccess($data);
        }else{
            return showError('不存在该商户');
        }
    }

    public function exchangeInformation(){
        $user_id = request()->UserModel->id;
        if($merchant_info = $this->M->where('user_id', $user_id)->find())
        {
            $infos = User::where('id', $user_id)->find();
            $type = 'score';
            //整理数据
            $data = [
                'main_title'=>'商家积分转化',
                'account'=> [
                    'account_id'=>'【商家ID:' . $merchant_info['id'] . '】',
                    'mobile'=>$infos['phone'],
                    'nickname'=>$merchant_info['title'],
                    'avatar'=>get_oss_url('/static/logo.png'),
                ],
                'transfer_account'=> [
                    'account_id'=>'【用户ID:' . $infos['id'] . '】',
                    'mobile'=>$infos['phone'],
                    'nickname'=>$infos['phone'],
                    'avatar'=>get_oss_url('/static/logo.png'),
                ],
                'texts'=> [
                    'transfer_amount'=>'可转化：' . calc_amount($merchant_info['score']),
                    'transfer_input'=>[
                        'name'=>'需转化金额',
                        'placeholder'=>'请输入金额',
                    ],
                    'transfer_tip'=>'预计收到金额',
                ],
                'tips'=> '',
                'formData'=>[
                    'amount'=>0,
                    'type'=>$type,
                    'username'=>2,
                    'pay_password'=>'',
                    'user_id'=>$user_id
                ],
                'amounts_texts'=>[
                    'amount'=>'转化金额',
                    'rate'=>'转化费率',
                    'tax'=>'手续费',
                    'arrival'=>'实际到账',
                    'need'=>'',
                ],
                'amounts'=>[
                    'amount'=>'',
                    'rate'=>0,
                    'tax'=>0,
                    'arrival'=>'',
                    'need'=>'',
                ]
            ];
            //响应数据
            return showSuccess($data);
        }else{
            return showError('不存在该商户');
        }
    }

    public function exchangeAction(){
        $params = request()->param();
        $currentUser = request()->UserModel;
        $user_id = $currentUser->id;

        if($merchant_info = $this->M->where('user_id', $user_id)->find())
        {
            $alias = request()->param('type');
            $amount = bcmul(request()->param('amount'), \app\model\Base::$hundred);

            if($amount > $merchant_info[$alias])
            {
                return showError('可转化额度不足');
            }

            $type = 'merchant_score_exchange';
            $isDone = \app\api\model\Merchant::afterExchange($user_id, $amount, $alias, $type);
            if($isDone === true){
                return showSuccess([], '转化成功');
            }else{
                return showError('转化失败');
            }
        }else{
            return showError('不存在该商户');
        }
    }
}